﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JBeamLibrary.Tools;
using JBeamLibrary.Types;

namespace JBeamLibrary.VehicleParts.Collections
{
    public class TaggedStringCollection : KeyedCollection<string, TaggedString>
    {
        #region CTORs

        /// <summary>Creates a new TaggedString collection</summary>
        public TaggedStringCollection() { }

        /// <summary>Creates a new TaggedString collection</summary>
        /// <param name="taggedStrings"></param>
        public TaggedStringCollection(IEnumerable<TaggedString> taggedStrings)
        {
            AddRange(taggedStrings);
        }

        #endregion

        #region Methods

        public void Add(string str, object tag)
        {
            Add(new TaggedString(str, tag));
        }

        public void AddRange(IEnumerable<TaggedString> taggedStrings)
        {
            foreach (var taggedString in taggedStrings)
                Add(taggedString);
        }

        public void Remove(object tag)
        {
            for (var i = 0; i < Count; i++)
            {
                var taggedString = this[i];
                if (!taggedString.Tag.SafeEquals(tag))
                    continue;

                RemoveAt(i);
                i--;
            }
        }

        #endregion

        #region KeyedCollection

        protected override string GetKeyForItem(TaggedString item)
        {
            return item.String;
        }

        #endregion
    }
}
